if (!window.ost)
	window.ost = new Object();


/**********************************************************************

	Timer
		callEvery(sec)
		callOnceAfter(sec)
		cancel()

**********************************************************************/
ost.Timer = function(callBack)
{
	this.callBack = callBack;
	this.intervalId = NaN;
	this.timeoutId = NaN;
}

ost.Timer.prototype.callEvery = function(sec)
{
	with (this) {
		cancel();
		intervalId = setInterval(callBack, sec*1000);
	}
}

ost.Timer.prototype.callOnceAfter = function(sec)
{
	with (this) {
		cancel();
		timeoutId = setTimeout(callBack, sec*1000);
	}
}

ost.Timer.prototype.cancel = function()
{
	with (this) {
		if (!isNaN(intervalId)) {
			clearInterval(intervalId);
			intervalId = NaN;
		}
		if (!isNaN(timeoutId)) {
			clearTimeout(timeoutId);
			timeoutId = NaN;
		}
	}
}




/**********************************************************************

	Navigator

**********************************************************************/
ost.Navigator = function()
{
}

ost.Navigator.prototype.language = function()
{
	if (!window.navigator.userLanguage)
		return window.navigator.language;	//Mozilla
	return window.navigator.userLanguage;
}




/**********************************************************************

	Cookies

**********************************************************************/
ost.Cookies = function(w)
{
	this.wnd = (w)?w:window;
}

ost.Cookies.prototype.enabled = function()
{
	return this.wnd.navigator.cookieEnabled;
}

ost.Cookies.prototype.get = function(name)
{
	if (this.enabled()) {
		var vals = this.wnd.document.cookie.split(";");
		for(var n=0; n<vals.length; ++n) {
			if (vals[n].match(name))
				return vals[n].substring(vals[n].indexOf("=")+1,vals[n].length);
		}
	}
	return "";
}

ost.Cookies.prototype.set = function(name,value)
{
	if (this.enabled()) {
		this.wnd.document.cookie = name + "=" + value;
		return true;
	}
	return false;
}

ost.Cookies.prototype.setWithExp = function(name,value,date)
{
	if (this.enabled()) {
		this.wnd.document.cookie = (name + "=" + value + "; expires=" + date.toGMTString());
		return true;
	}
	return false;
}




/**********************************************************************

	BrowserCoordinates

**********************************************************************/
ost.BrowserCoordinates = function(w)
{
	this.wnd = (w)?w:window;
	this.ChromeX = this.ChromeY = this.x = this.y = 0;
	with(this) {
		if (!findInCookie()) {
			detect();
			saveInCookie();
		}
		x = screenLeft() - ChromeX;
		y = screenTop() - ChromeY;
	}
}

ost.BrowserCoordinates.prototype.screenLeft = function()
{
	if (!this.wnd.screenLeft)
		return this.wnd.screenX;		//Mozilla
	return this.wnd.screenLeft;
}

ost.BrowserCoordinates.prototype.screenTop = function()
{
	if (!this.wnd.screenTop)
		return this.wnd.screenY;
	return this.wnd.screenTop;
}

ost.BrowserCoordinates.prototype.detect = function()
{
	with(this) {
		var intitX = screenLeft(), intitY = screenTop();
		this.wnd.moveTo(0,0);
		ChromeX = screenLeft(); 
		ChromeY = screenTop();
		this.wnd.moveTo(intitX-ChromeX,intitY-ChromeY);
	}
}

ost.BrowserCoordinates.prototype.browserName = function()
{
	return (!this.wnd.screenLeft)? "Mozilla" : "MSIE";
}

ost.BrowserCoordinates.prototype.findInCookie = function()
{
	var oC = new ost.Cookies(this.wnd);
	this.ChromeX = parseInt(oC.get(this.browserName()+"X"));
	this.ChromeY = parseInt(oC.get(this.browserName()+"Y"));
	return (!isNaN(this.ChromeX) && !isNaN(this.ChromeX));
}

ost.BrowserCoordinates.prototype.saveInCookie = function()
{
	var oC = new ost.Cookies(this.wnd);
	var date = new Date();
	date.setMonth(date.getMonth()+1);
	oC.setWithExp(this.browserName()+"X",this.ChromeX,date);
	oC.setWithExp(this.browserName()+"Y",this.ChromeY,date);
}




/************************************************************************

	DynLoader
		DynLoader(wndUrl, url, callBack)		
		request(oInParams)
		--
		onRequestComplete(oOutParams) -> callBack(oOutParams)
		
		oParams = {'param1':'value1', 'param2':'value2', ... }
		
************************************************************************/
ost.DynLoader = function(wndName, url, callBack)
{
	if (!arguments.length)
		return;
		
	window["ost:DynLoader"] = this;
	
	this.callBack = callBack;
	this.isLocked = false;

	this.oForm = document.createElement('FORM');
	this.oForm.style.visibility = 'hidden';
	this.oForm.style.width = this.oForm.style.height = 0;
	this.oForm.target = wndName;
	this.oForm.action = url;
	this.oForm.method = 'POST';
	document.body.appendChild(this.oForm);
	
	this.requests = new Array;
}

ost.DynLoader.prototype.request = function(params)
{
	if (this.requests.push(params) == 1 && !this.isLocked)
		this.sendRequest();
}

ost.DynLoader.prototype.onRequestComplete = function(params)
{
	with (this) {
		lock();
		callBack(params);
		unlock();
		if (requests.length)
			sendRequest();
	}
}

ost.DynLoader.prototype.sendRequest = function(params)
{
	with (this) {
		var params = requests[0];
		requests.shift();
		
		var oE = null;
		while ((oE=oForm.firstChild))
			oForm.removeChild(oE);
			
		for (nm in params) {
			oE = document.createElement('INPUT');
			oE.type = 'text';
			oE.name = nm;
			oE.value = params[nm];
			oForm.appendChild(oE);
		}
		
		oForm.submit();
	}
}

ost.DynLoader.prototype.lock = function()
{
	this.isLocked = true;
}

ost.DynLoader.prototype.unlock = function()
{
	this.isLocked = false;
}


function syncFrame() 
{
	window.parent.gHeader.scrollTo(document.body.scrollLeft,0);
	window.parent.gRowHeader.scrollTo(0,document.body.scrollTop);
}

function new_window(url,width,height,noMove)
{
	var oW = window.open(url,
		 				 '_blank',
		 				 'location=no,menubar=no,status=no,toolbar=no,resizable=yes');

	var x=0,y=0;
	if (width && height) {
		x = Math.round((screen.availWidth - width) / 2);
  		y = Math.round((screen.availHeight - height) / 2);
		oW.moveTo(x,y);
		oW.resizeTo(width,height);
	} 
	else if (!noMove) {
		var coordinates = new ost.BrowserCoordinates(window.top);
		oW.moveTo(coordinates.x,coordinates.y);
	}
	
	return oW;
}

function make_top_most() 
{
	if( window.top != window )
		window.top.location = window.location;
}

function getChildrenByTagName(ob,tagName)
{
	var elems = ob.getElementsByTagName(tagName), children = new Array;
	for(var i=0,j=0; i<elems.length; i++)
		if( elems[i].parentNode == ob )
			children[j++] = elems[i];
	return children;
}

function el(id) { return document.getElementById(id); }
function val(id) { return el(id).value; }

//Field types
var ftString	= 0,
	ftRaw		= 1,
	ftInteger	= 2,
	ftFloat		= 3,
	ftTime		= 4,
	ftAmount	= 5,
	ftRecord	= 6;


function show_properties(obj,name,obView)
{
	var s = new String;
	for( var nm in obj ) {
		if( name && name!=nm )
			continue;
		s += nm + ": " + obj[nm];
		if( !obView ) s += "\n";
		else s += "<br>";
	}
	if( obView )
		obView.innerHTML = s;
	else
		alert(s);		
}




/***************************************************************************

	Debug
		assert(bCond, msg)
		check(bCond, msg)
		write(string/Error)
		
	<body onload="debug.open()" onunload="debug.close()">

***************************************************************************/
ost.Debug = function()
{
}

ost.Debug.prototype.open = function()
{
	this.oDebugWnd = new_window('',600,200,true);
}

ost.Debug.prototype.close = function()
{
	if (this.oDebugWnd)
		this.oDebugWnd.close();
}

ost.Debug.prototype.assert = function(bCond, msg) 
{
	if (!bCond)
		throw new Error(msg);
}

ost.Debug.prototype.check = function(bCond, msg) 
{
	if (!bCond)
		this.writeInHighPriority(msg);
}

ost.Debug.prototype.write = function(val)
{
	with (this) {
		if (oDebugWnd) {
			if (typeof(val) == 'undefined')
				oDebugWnd.document.write('<br>');
			else if (typeof(val) == 'object') {
				if (Error.prototype.isPrototypeOf(val))
					writeInHighPriority(
						"Exception occured<br>" + 
						val.name + " [" + (val.number & 0xFFFF) + "]:<br>" +
						val.message + "<br>" + val.description + "<br>"
					);
			} 
			else
				oDebugWnd.document.write(new String(val) + '<br>');
				
			oDebugWnd.scrollTo(0,9000000000);
		}
	}
}

ost.Debug.prototype.writeInHighPriority = function(msg)
{
	this.oDebugWnd.document.writeln("<font color=red>"+msg+"</font><br>");
	this.oDebugWnd.scrollTo(0,9000000000);
}

var debug = new ost.Debug;